<?php

class BaseController extends Controller {

	/**
	 * Setup the layout used by the controller.
	 *
	 * @return void
	 */
	protected function setupLayout()
	{
		if ( ! is_null($this->layout))
		{
			$this->layout = View::make($this->layout);
		}
	}

	/**
     * PHP CURL get请求
     *
     * @param uri     请求地址
     * @return
     */
    public function curl_get( $uri , $token = "" , $host = "" , $auth = "" ){
        $time = time();
        $header = array(
            'clientdate: '.$time ,
            'ptimeauth: '.$token ,
            );
		if( $host != '' ){
			$header[] = 'Host: '.$host;
		}
		if( $auth != '' ){
			$header[] = 'authorization: '.$auth;
		}
        $temp_uri = $uri;
        $ch = curl_init();
        curl_setopt ( $ch, CURLOPT_URL, $temp_uri );
        curl_setopt ( $ch, CURLOPT_HEADER, 0 );
        if( $token != "" || $auth != '' || $host != '' ){
            curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );
        }
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

    /**
     * PHP CURL 模块发送post请求
     *
     * @param uri     请求地址
     * @param data    所需发送的数据
     * @return
     */
    public function curl_post( $uri , $data , $host = "" , $auth = "" ){
        $temp_uri = $uri;
        $temp_data = http_build_query($data);
        $ch = curl_init();

		$header = array();
		if( $host != '' ){
			$header[] = 'Host: '.$host;
		}
		if( $auth != '' ){
			$header[] = 'authorization: '.$auth;
		}
        curl_setopt ( $ch, CURLOPT_URL, $temp_uri );
        curl_setopt ( $ch, CURLOPT_POST, 1 );
		curl_setopt ( $ch, CURLOPT_HEADER, 0 );
		if( $auth != "" || $host != '' ){
            curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );
        }
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        //curl_setopt ( $ch, CURLOPT_HTTPHEADER, array("Expect:") );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $temp_data );
        $temp_post_result = curl_exec ( $ch );
        curl_close ( $ch );
        return $temp_post_result;
    }

}
